package com.socialmediaanalytics.client.facebook;

import java.util.HashMap;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.socialmediaanalytics.client.GWTEntryPoint;
import com.socialmediaanalytics.client.facebook.sdk.FBCore;
import com.socialmediaanalytics.client.facebook.sdk.JSOModel;
import com.socialmediaanalytics.client.interests.ShowMe;
import com.socialmediaanalytics.client.model.User;

public class FacebookFriends {

	public static HashMap<String, String> facebookFriendsID = new HashMap<String, String>();

	public FacebookFriends(final FBCore fbCore) {

		fbCore.api("/me/friends", new AsyncCallback<JavaScriptObject>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(JavaScriptObject result) {

				JSOModel jso = result.cast();

				JsArray array = jso.getArray("data");

				for (int i = 0; i < array.length(); i++) {

					JSOModel j = array.get(i).cast();

					String id = j.get("id");

					String name = j.get("name");

					User friend = new User(id, name);

					GWTEntryPoint.facebookFriends.put(id, friend);

					facebookFriendsID.put(name, id);

					new FacebookFriendLikes(fbCore, id);

					new FacebookFriendMovies(fbCore, id);

					new FacebookFriendBooks(fbCore, id);

					if (i == (array.length() - 1) || i % 100 == 0) {

						// ShowCommonInterests.show();
					}

					String interests = "";
					if (friend.getFacebookInterests() != null) {

						interests += friend.getFacebookInterests();
					}

					GWTEntryPoint.facebookUser.setFacebookInterests(interests
							+ "<br>friends: " + array.length());

					new ShowMe();
				}
			}
		});

	}

}
